package org.cige.ws.edu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.cige.ws.edu.config.MinioConfig;
import org.cige.ws.edu.model.FFileReordFileQuery;
import org.cige.ws.edu.model.FFileReordQuery;
import org.cige.ws.edu.model.PageQuery;
import org.cige.ws.edu.model.entity.FFileReord;
import org.cige.ws.edu.model.entity.FileModule;
import org.cige.ws.edu.service.FileReordService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.InputStream;

/**
 * 文件管理
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {


    @Resource
    private FileReordService fileReordService;


    @Autowired
    private MinioClient minioClient;


    @Autowired
    private MinioConfig minioConfig;
    /**
     * 查询资源中心
     * @param page 分页信息
     * @return
     */
    @PostMapping("/listPageFile")
    public Result<IPage<FFileReord>> listPageFile(@RequestBody PageQuery<FFileReordFileQuery> page){
        return Result.ok(fileReordService.listPageFile(page));
    }

    /**
     * 查询资源库
     * @param page 分页信息
     * @return
     */
    @PostMapping("/listPage")
    public Result<IPage<FFileReord>> listPage(@RequestBody PageQuery<FFileReordQuery> page){
        return Result.ok(fileReordService.listPage(page));
    }


    /**
     * 查询常用模版库
     * @param page 分页信息
     * @return
     */
    @PostMapping("/listPageModule")
    public Result<IPage<FileModule>> listPageModule(@RequestBody Page page){
        return Result.ok(fileReordService.listPageModule(page));
    }

    /**
     * @Description:检查文件对象是否存在

     */
    public boolean existFile(String fileKey) throws Exception {
        ObjectStat objectStat = minioClient.statObject(minioConfig.getBucketName(), fileKey);
        if (objectStat != null && objectStat.length() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 下载指定文件
     * @param fileKey 文件唯一标识
     * @return
     */
    @GetMapping("/download")
    public void download(String fileKey, ServletOutputStream outputStream){
        boolean flag = false;
        fileKey=fileKey.substring(fileKey.lastIndexOf("temp/"),fileKey.length());
        try {
            flag = existFile(fileKey);
            if (!flag) {
                throw new JeecgBootException("文件不存在");
            }
        } catch (Exception e) {
            throw new JeecgBootException(e);
        }
        try (InputStream inputStream = minioClient.getObject(minioConfig.getBucketName(), fileKey)) {
            IOUtils.copy(inputStream, outputStream);
        } catch (Exception e) {
            log.error("文件下载异常");
            e.printStackTrace();
        }
    }


}
